package com.qxzn.app.medicalhealth.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.bean.ContactsBean;
import com.qxzn.app.bean.MedicalHealthBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;
import com.qxzn.app.util.DisplayUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FamilyMedicalRecordsHeadAdapter extends PagerAdapter {

    private Context mContext;
    private View.OnClickListener listener;
    private List<ContactsBean.DataBean> mDataList;
    private ContactsBean.DataBean item;

    public FamilyMedicalRecordsHeadAdapter(Context context, View.OnClickListener listener) {
        this.mContext = context;
        this.listener = listener;
    }

    public void setData(List dataList) {
        this.mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (this.mDataList == null) {
            return 0;
        }
        return this.mDataList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View page = getView(position);
        container.addView(page);
        return page;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View) {
            container.removeView((View) object);
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public View getView(final int position) {
        View view = null;
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_family_medical_records_head, null);
            holder = new ViewHolder(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        item = mDataList.get(position);
        holder.tvNickname.setText(item.getMemName());
        Glide.with(mContext).load(item.getHeadportrait()).apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.logo).error(R.mipmap.logo).override(DisplayUtil.dip2px(mContext, 70))).into(holder.ivHead);
        holder.llItem.setTag(position);
        holder.llItem.setOnClickListener(listener);
        return view;
    }

    static class ViewHolder {

        @BindView(R2.id.iv_head)
        ImageView ivHead;
        @BindView(R2.id.tv_nickname)
        TextView tvNickname;
        @BindView(R2.id.ll_item)
        LinearLayout llItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
